package com.ljh.mybatisplusdemo.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import java.nio.charset.StandardCharsets;

/**
 * @author: Themberfue
 * @date: 2025/3/20 18:43
 * @description:
 */
@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override // ? 请求拦截器
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("请求拦截器执行...");
        // true 放行，false 拦截
        response.setStatus(401);
        String msg = "被拦截立";
        response.getOutputStream().write(msg.getBytes(StandardCharsets.UTF_8));
        return true;
    }

    @Override // ? 响应拦截器
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        log.info("响应拦截器执行...");
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override // ? 视图加载完后执行
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        log.info("afterCompletion...");
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
